package com.greenline.push.home.controllers.message;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.MsgForSendManager;
import com.greenline.push.dal.pushcenter.dataobject.MsgForSendDO;
import com.greenline.push.dal.query.MsgForSendQuery;

@Controller
@RequestMapping(value = "/order/schedule")
public class MsgForSendController {

    @Resource
    private MsgForSendManager msgForSendManager;
    private final Log logger = LogFactory.getLog(MsgForSendController.class);

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "msgforsend/index";
    }

    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<MsgForSendDO> listRecord(MsgForSendQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<MsgForSendDO> ret = new GridJsonData<MsgForSendDO>();
        ret.setQuery(msgForSendManager.listByQuery(query));
        return ret;
    }

    @RequestMapping(value = "/reset", method = RequestMethod.GET)
    public @ResponseBody
    BaseJson add(ModelMap model) {
        BaseJson json = new BaseJson();
        try {
            if (msgForSendManager.resetStatus()) {
                json.setMessage("重置成功！");
            } else {
                json.setHasError(true);
                json.setMessage("重置出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }
}
